package com.serkol.ams.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.ams.bean.MyPage;

import com.serkol.ams.entity.Goods;
import com.serkol.ams.mapper.GoodsMapper;
import com.serkol.ams.modal.SqlFeildModal;
import com.serkol.ams.service.GoodsService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author pingan
 * @description 针对表【goods】的数据库操作Service实现
 * @createDate 2022-09-19 16:34:25
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods>
        implements GoodsService {

    @Override
    public MyPage<Map<String, Object>> findByList(int pageSize, int page1) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(SqlFeildModal.getGoodsBackListFeild());
        queryWrapper.orderByDesc("ID");
        Page<Map<String, Object>> page = new Page<>(page1, pageSize);
        Page<Map<String, Object>> mapPage = this.baseMapper.selectMapsPage(page, queryWrapper);
        return MyPage.parsePage(mapPage);
    }

    @Override
    public MyPage<Map<String, Object>> findByCloudPageList(int pageSize, int current) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(SqlFeildModal.getGoodsBackCloudFeild());
        //    queryWrapper.lambda().eq(Goods::getCloudType,1);
        queryWrapper.orderByDesc("ID");
        Page<Map<String, Object>> page = new Page<>(current, pageSize);
        Page<Map<String, Object>> mapPage = this.baseMapper.selectMapsPage(page, queryWrapper);
        return MyPage.parsePage(mapPage);
    }

    @Override
    public MyPage<Map<String, Object>> findByPageIntegralList(int pageSize, int current) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(SqlFeildModal.getGoodsBackIntegralListFeild());
        queryWrapper.lambda().eq(Goods::getIsIntegral, 1);
        queryWrapper.orderByDesc("ID");
        Page<Map<String, Object>> page = new Page<>(current, pageSize);
        Page<Map<String, Object>> mapPage = this.baseMapper.selectMapsPage(page, queryWrapper);
        return MyPage.parsePage(mapPage);
    }

    @Override
    public Map<String, Object> findByMapId(long id) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(SqlFeildModal.getGoodsPosterFeild());
        queryWrapper.lambda().eq(Goods::getId, id);
        queryWrapper.orderByDesc("ID").last("LIMIT 1");
        Map<String, Object> map = this.getMap(queryWrapper);

        return map;
    }

    @Override
    public List<Map<String, Object>> findGoodsList(Long agentId) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(SqlFeildModal.getGoodsBackNewListFeild(agentId));

        queryWrapper.orderByDesc("ID");
        return this.listMaps(queryWrapper);
    }

    @Override
    public List<Map<String, Object>> findGoodsAiList(Long agentId) {
        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(SqlFeildModal.getGoodsBackFeild(agentId));

        queryWrapper.orderByDesc("ID");
        return this.listMaps(queryWrapper);
    }

    @Override
    public List<Map<String, Object>> homeGoodsChartStastic(int type) {
        Date date = new Date();

        QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
        queryWrapper.select(SqlFeildModal.getLoginLogStatastica());
        queryWrapper.groupBy("ymd").orderByAsc("name");
        date = switch (type) {
            case 1 -> DateUtils.addMonths(date, -1);
            case 2 -> DateUtils.addMonths(date, -2);
            default -> date;
        };
        queryWrapper.apply("DATE_FORMAT(create_time,'%Y-%m') = DATE_FORMAT(STR_TO_DATE('{0}', '%Y-%m'), '%Y-%m')",
                new SimpleDateFormat("yyyy-MM").format(date));
        return this.listMaps(queryWrapper);

    }
}




